<?php
class CityController extends Zend_Controller_Action {
	// Mảng tham số nhận được ở mỗi Action:
	protected $_arrParam;
	private $USER_ID = 0;
	private $URI = "/";
	private $URL_ACC_LOGIN = "/account/login/";
	private $DEFAULT_PROFILE_PICTURE = "default/img/no_image.jpg";
	private $ALERT_TYPE_ERROR = "alert-error";
	private $ALERT_TYPE_SUCCESS = "alert-success";
	public function init() {
		$this->_arrParam = $this->_request->getParams ();
		// Truyền ra view
		$this->view->arrParam = $this->_arrParam;
		
		// Xu li cho phan header
		$this->URI = $this->getRequest ()->getScheme () . "://" . $this->getRequest ()->getHttpHost () . $this->getRequest ()->getRequestUri ();
		$mAccount = new Default_Model_Account ();
		if (! $mAccount->hasIdentity ()) {
			$this->_redirect ( $this->URL_ACC_LOGIN . '?return_url=' . $this->URI );
		} else {
			// get user info
			$this->USER_ID = $mAccount->getUserId ();
			
			$this->view->fullname = $mAccount->getFullName ();
			$this->view->return_url = $this->URI;
			$this->view->userid = $mAccount->getUserId ();
			$strAvatar = $mAccount->getProfilePicture ();
			if ($strAvatar != "") {
				$this->view->my_avatar = $mAccount->getProfilePicture ();
			} else {
				$this->view->my_avatar = $this->DEFAULT_PROFILE_PICTURE;
			}
			// get report
			$mRep = new Default_Model_Report();
			$arrRep = $mRep->listAllReport();
			$this->view->arrRep = $arrRep;
		}
		// End xu li cho phan header
	}
	public function indexAction() {
		$mCity = new Default_Model_City();
		
		if($this->_request->isPost()) {
			if(isset($_POST['btnAdd'])) {
				$arrParam = array(
						'name' => ($_POST['city_name']),
						'created_time' => date('Y-m-d H:i:s')
				);
				if($mCity->insertCity($arrParam)) {
					$this->view->display_alert = 1;
					$this->view->alert_type = $this->ALERT_TYPE_SUCCESS;
					$this->view->alert_message = 'Thêm mới tỉnh/thành phố thành công!';
				} else {
					$this->view->display_alert = 1;
					$this->view->alert_type = $this->ALERT_TYPE_ERROR;
					$this->view->alert_message = '<strong>Error!</strong> '.$mCity->getError();
				}
			} else if(isset($_POST['btnEdit']) && isset($_POST['city_id'])) {
				$arrParam = array(
						'name' => ($_POST['city_name']),
						'updated_time' => date('Y-m-d H:i:s')
				);
				if($mCity->updateCityInfo($arrParam, $_POST['city_id'])) {
					$this->view->display_alert = 1;
					$this->view->alert_type = $this->ALERT_TYPE_SUCCESS;
					$this->view->alert_message = 'Chỉnh sửa tỉnh/thành phố thành công!';
				} else {
					$this->view->display_alert = 1;
					$this->view->alert_type = $this->ALERT_TYPE_ERROR;
					$this->view->alert_message = '<strong>Error!</strong> '.$mCity->getError();
				}
			}
		} else if ($this->_request->isGet ()) {
			// delete city
			if (isset($_GET['delete']) && $_GET['delete'] == 1) {
				if(isset($_GET['city_id'])) {
					// delete user
					$mProcess = new Default_Model_YourTourProcess();
					$strMessReturn = $mProcess->deleteCityInfo($_GET['city_id']);
					if($strMessReturn == "") {
						$this->view->display_alert = 1;
						$this->view->alert_type = $this->ALERT_TYPE_SUCCESS;
						$this->view->alert_message = 'Xoá tỉnh/thành phố thành công!';
					} else {
						$this->view->display_alert = 1;
						$this->view->alert_type = $this->ALERT_TYPE_ERROR;
						$this->view->alert_message = '<strong>Error!</strong> '.$strMessReturn;
					}
				} else {
					$this->view->display_alert = 1;
					$this->view->alert_type = "";
					$this->view->alert_message = '<strong>Warning!</strong> Thiếu thông tin để xoá.';
				}
			}
		}
		// process to display view
		$this->view->total_city = $mCity->countCity();
		$this->view->my_user_id = $this->USER_ID;
		$arrCity = $mCity->listAllCityPaging();
		if (count ( $arrCity ) != 0) {
			// paging
			$adapter = new Zend_Paginator_Adapter_DbSelect ( $arrCity );
			$paginator = new Zend_Paginator ( $adapter );
			$paginator->setItemCountPerPage ( 100 );
			$paginator->setPageRange ( 3 );
			$currentPage = $this->_request->getParam ( 'page', 1 );
			$paginator->setCurrentPageNumber ( $currentPage );
			$this->view->arrCity = $paginator;
		}
	}
}